package com.touch.jgloo;
import javax.media.opengl.*;
import javax.media.opengl.glu.GLU;

import com.touch.jgloo.core.JGLObject;
/**
 * Defines the DefaultView for GLCanvas. This sets up everything for a 3D game's view.
 * @author Homicidal Monkey
 * @version 10.1.9
 * @since r461
 * @see com/touch/Licence
 */
public class JGLDefaultView implements GLEventListener 
{
	//GLU instance.
	private GLU glu = new GLU();
	/**
	 * XRotation of the camera
	 * @since r461
	 */
	public static float ROTATE_X;
	/**
	 * YRotation of the camera
	 * @since r461
	 */
	public static float ROTATE_Y;
	/**
	 * ZRotation of the camera
	 * @since r461
	 */
	public static float ROTATE_Z;
	/**
	 * Zoom of the camera
	 * @since r461
	 */
	public static float ZOOM;
	
	public void display(GLAutoDrawable drawable) 
	{	
		//get the GL instance
		final GL gl = drawable.getGL();
		//load the GL-Identity 
		gl.glLoadIdentity();
		//clear the buffer
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
		//rotate the camera
		gl.glRotatef(ROTATE_X, 1.0f, 0.0f, 0.0f);
		gl.glRotatef(ROTATE_Y, 0.0f, 1.0f, 0.0f);
		gl.glRotatef(ROTATE_Z, 0.0f, 0.0f, 1.0f);
		//set the zoom value
		gl.glTranslatef(50.0f, 0.0f, ZOOM);
	}
	public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {}

	public void init(GLAutoDrawable drawable) 
	{
		//get the GL object
		final GL gl = drawable.getGL();
		
		//set the shading mode
		gl.glShadeModel(GL.GL_SMOOTH);
		//Set the background color to black
		gl.glClearColor(0f, 0f, 0f, 0f);
		//Set the depth to 1f
		gl.glClearDepth(1.0f);
		//Enable the use of the z-axis
		gl.glEnable(GL.GL_DEPTH_TEST);	
		//perform LEQUAL depth test
		gl.glDepthFunc(GL.GL_LEQUAL);  
		//Enable using textures
		gl.glEnable(GL.GL_TEXTURE_2D);
		//Enable color blending
		gl.glEnable(GL.GL_BLEND);
		//Enable alpha blending
		gl.glEnable(GL.GL_ALPHA);
		//more high-quality
		gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
		//more high-quality
		gl.glHint(GL.GL_POLYGON_SMOOTH_HINT, GL.GL_NICEST);		
		gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
		JGLObject.GL_INSTANCE = gl;
	}
	public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height) {
        final GL gl = gLDrawable.getGL();

        if (height <= 0) // avoid a divide by zero error!
            height = 1;
        final float h = (float) width / (float) height;
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(45.0f, h, 0.0, 100.0f);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
    }
}
